/*
 * @Author: 杨三炮
 * @Date: 2021-11-21 12:39:38
 * @LastEditors: 杨三炮
 * @LastEditTime: 2021-11-21 15:28:15
 * @FilePath: \node\node\config\passport.js
 */
const JwtStrategy = require('passport-jwt').Strategy, ExtractJwt = require('passport-jwt').ExtractJwt;
const db = require('../model/model')
const opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = 'secret';

module.exports = passport => {
    passport.use(new JwtStrategy(opts, (jwt_payload, done) => {
        let sql = ''
        if (jwt_payload.id.toString().slice(0, 4) == '2021') {
            sql = `SELECT *FROM teacher where id=${jwt_payload.id}`
        } else {
            sql = `SELECT *FROM student where id=${jwt_payload.id}`
        }

        db.query(sql).then(user => {
            if (!user.length) {
                return done(null, false)
            }
            return done(null, user[0])
        }).catch(err => console.log(err))
    }));
}